var ResultLayer = cc.Layer.extend({
    panel: null,
    ctor: function(isWin){
        this._super();
        this.init(isWin);
    },
    init: function (isWin) {
        if (isWin) {
            this.panel = new cc.Sprite(res.Succeed_png);
        }
        else {
            this.panel = new cc.Sprite(res.Failed_png);
        }
        this.panel.x = cc.winSize.width/2;
        this.panel.y = cc.winSize.height/2 + 50;
        this.addChild(this.panel);

        var size = this.panel.getContentSize();

        if (isWin) {
            var step = mainLayer.step;
            var percent;
            if(step < 5){
                percent = 95;
            }else{
                if(step < 35){
                    percent = 95 - (step-5)*3;
                }else{
                    percent = 5;
                }
            }
            var label = new cc.LabelTTF("继续刷屏！\n"+step+"步推倒我的小羊驼\n打败"+percent+"%朋友圈的人！\n你能超过我吗？",  'Times New Roman', 20);
            share("继续刷屏！\n"+step+"步推倒我的小羊驼\n打败"+percent+"%朋友圈的人！\n你能超过我吗？");

        }
        else {
            var label = new cc.LabelTTF("我滴小羊驼呀它又跑掉了\nT_T 快帮我抓回来！",  'Times New Roman', 20);
            share("我滴小羊驼呀它又跑掉了\nT_T 快帮我抓回来！");
        }
        label.width = size.width;
        label.color = cc.color(0,0,0,255);
        label.textAlign = cc.LabelTTF.TEXT_ALIGNMENT_CENTER;
        label.setPosition(size.width/2,size.height/4);
        this.panel.addChild(label);


        var notify = new cc.Sprite(res.Notify_png);
        notify.setPositionX(this.panel.getPositionX() - notify.getContentSize().width/2 - 10);
        notify.setPositionY(this.panel.getPositionY() - this.panel.getContentSize().height / 2 - notify.getContentSize().height / 2);
        this.addChild(notify);

        var replay = new cc.Sprite(res.Replay_png);
        replay.setPositionX(this.panel.getPositionX() + replay.getContentSize().width/2 + 10);
        replay.setPositionY(this.panel.getPositionY() - this.panel.getContentSize().height / 2 - replay.getContentSize().height / 2);
        this.addChild(replay);

        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: function (touch, event) {
                return this.isVisible();
            }.bind(this),
            onTouchEnded: function (touch, event) {
                var touchPos = touch.getLocation();

                if(this.isClickSprite(touchPos,replay) == true){
                    mainLayer.changeState(STATE_PLAY);
                    mainLayer.removeChild(this);
                }

                if(this.isClickSprite(touchPos,notify) == true){
                    var shareLayer = new ShareLayer();
                    mainLayer.addChild(shareLayer,5);
                }
            }.bind(this)
        }, this);
    },
    isClickSprite: function(touchPos,node){
        var btnPos = node.getPosition();
        var btnSize = node.getContentSize();
        var minPos = cc.p(btnPos.x - btnSize.width/2 , btnPos.y - btnSize.height/2);
        var maxPos = cc.p(btnPos.x + btnSize.width/2 , btnPos.y + btnSize.height/2);
        if(touchPos.x < minPos.x) return false;
        if(touchPos.y < minPos.y) return false;
        if(touchPos.x > maxPos.x) return false;
        if(touchPos.y > maxPos.y) return false;
        return true;
    }
    
});